<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/4/11
 * Time: 11:11
 */

namespace Home\Controller;


class CheckAuthController extends CommonController
{
    //@todo 加载用户组列表页
    public function index(){
        $this->display();
    }
    //@todo 新增管理组方法
    public function add_role(){
        $title=$_POST['title'];
        $addData=array(
            'title'=>$title
        );
        $id=D('auth_group')->add($addData);
        if(!$id){
            $this->ajaxReturn(array('success'=>false,'message'=>'新增失败'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'新增成功'));
    }
    //@todo 删除用户组
    public function delGroup(){
        $id=$_GET['id'];
        $del=D('auth_group')->delete($id);
        if(!$del){
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
    }
    //@todo 获取用户组列表
    public function getAuthGroupList(){
        $count=D('auth_group')->count();
        $list=D('auth_group')->limit($_GET['offset'],$_GET['limit'])->select();
        $this->ajaxReturn(array('rows'=>$list,'total'=>$count));
    }

    //@todo  获取该用户组下的用户
    public function getUser(){
        $list=D('auth_group_access')->where(array('group_id'=>$_GET['id']))->select();
        if($list){
            $user_id=array_column($list,'uid');
            $user_list=D('user')->field("*,FROM_UNIXTIME(ac_time) as ac_time")
                ->where(array('id'=>array('in',$user_id)))->order('id desc')->select();
            echo json_encode($user_list);
        }else{
            echo json_encode(array());
        }


    }
    //@todo 加载权限列表页
    public function auth_list(){
        $model=D('auth_module')->select();
        $this->assign('module',$model);
        $this->display();
    }
    //@todo 新增权限列表
    public function addRule(){
        $data=$_POST;
        $id=D('auth_rule')->add($data);
        if(!$id){
            $this->ajaxReturn(array('success'=>false,'message'=>'新增失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'新增成功'));
    }
    //@todo 获取模块列表
    public function getModule(){
        $count=D('auth_module')->count();
        $data=D('auth_module')->select();
        $this->ajaxReturn(array('total'=>$count,'rows'=>$data));
    }
    //@todo 获取模块下的权限
    public function getAuthRuleList(){

        $data=D('auth_rule')->field("*,wx_auth_rule.id as id")
            ->join('left join wx_auth_module as am on am.id=wx_auth_rule.type_module')
            ->where(array('type_module'=>I('get.id')))->select();
        $this->ajaxReturn($data);
    }

    //@todo 删除权限
    public function delRule(){
        $id=$_GET['id'];
        $del=D('auth_rule')->delete($id);
        if(!$del){
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
    }

    //@todo 加载分配权限页面
    public function giveRule(){
        $id=$_GET['id'];
        $group=D('auth_group')->find($id);

        //@todo 查询 权限
        $model=D('auth_module')->select();
        foreach($model as $key =>$value){
            $model[$key]['list_rule']=D('auth_rule')->where(array('type_module'=>$value['id']))->select();
        }
        $this->assign('groupInfo',$group);
        $this->assign('ruleArray',$group['rules']);
        $this->assign('rule',$model);
        $this->display();
    }
    //@todo 分配权限
    public function save_rule(){
        $data=I('post.');
        $saveData=array(
            'id'=>$data['id'],
            'rules'=>implode(',',$data['rule'])
        );
       $save=D('auth_group')->save($saveData);
        if(!is_numeric($save)){
            $this->ajaxReturn(array('success'=>false,'message'=>'分配失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'分配成功'));
    }

}